<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="门禁名称" prop="accessName">
        <el-input
          v-model="queryParams.accessName"
          placeholder="请输入门禁名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['access:access:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['access:access:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete(2)"
          v-hasPermi="['access:access:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:user:export']"
            >导出打卡记录</el-button>
          </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="区域名称" align="center" prop="name" />
      <el-table-column label="设备IP" align="center" prop="accessIp" />
      <el-table-column label="门禁名称" align="center" prop="accessName" />
      <el-table-column label="账号" align="center" prop="loginname" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="handlePeople(scope.row)"
            >查看人员</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-location-information"
            @click="handleRecords(scope.row)"
            >打卡记录</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['access:access:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row, 1)"
            v-hasPermi="['access:access:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="门禁名称" prop="accessName">
          <el-input v-model="form.accessName" placeholder="请输入门禁名称" />
        </el-form-item>
        <el-form-item label="门禁IP" prop="accessIp">
          <el-input v-model="form.accessIp" placeholder="请输入门禁IP" />
        </el-form-item>
        <el-form-item label="账号" prop="loginname">
          <el-input v-model="form.loginname" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <!-- <el-form-item label="ID" prop="accessId" style="display: none;">
          <el-input v-model="form.accessId" placeholder="请输入ID" />
        </el-form-item> -->
        <el-form-item label="区域选择">
          <el-select v-model="form.region" placeholder="请选择区域">
            <el-option
              v-for="item in postOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer
      title="人员详情"
      :visible.sync="table"
      direction="rtl"
      size="50%"
    >
      <el-table :data="peopleList">
        <el-table-column property="name" center label="姓名"></el-table-column>
        <el-table-column
          property="employeeNo"
          center
          label="工号"
        ></el-table-column>
        <el-table-column property="userType" center label="人员类型">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_people_status"
              :value="scope.row.userType"
            />
          </template>
        </el-table-column>
        <el-table-column
          property="Valid.beginTime"
          center
          label="开始时间"
        ></el-table-column>
        <el-table-column
          property="Valid.endTime"
          center
          label="结束时间"
        ></el-table-column>
      </el-table>
      <pagination
        v-show="totalPeople > 0"
        :total="totalPeople"
        :page.sync="recordsQuery2.pageNum"
        :limit.sync="recordsQuery2.pageSize"
        @pagination="getList3"
      />
    </el-drawer>
    <el-drawer
      title="人员详情"
      :visible.sync="table2"
      direction="rtl"
      size="50%"
    >
    <el-form
      :model="recordsQuery"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
    <el-form-item label="姓名" prop="name">
        <el-input
          v-model="recordsQuery.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery2"
        />
      </el-form-item>
      <el-form-item label="工号" prop="employeeNo">
        <el-input
          v-model="recordsQuery.employeeNo"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery2"
        />
      </el-form-item>
      <el-form-item label="时间">
        <el-date-picker
      v-model="value2"
      type="datetimerange"
      :picker-options="pickerOptions"
      value-format="yyyy-MM-dd HH:mm:ss"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      align="right">
    </el-date-picker>
          </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery2"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
      <el-table :data="recordsList">
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column property="name" align="center" label="姓名"></el-table-column>
        <el-table-column
          property="EmployeeNo"
          align="center"
          label="工号"
        ></el-table-column>
        <el-table-column property="time" align="center" label="时间">
        </el-table-column>
        <el-table-column property="type" align="center" label="类型">
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalRecords > 0"
        :total="totalRecords"
        :page.sync="recordsQuery.pageNum"
        :limit.sync="recordsQuery.pageSize"
        @pagination="getList2"
      />
    </el-drawer>
  </div>
</template>

<script>
import {
  listGas,
  addPost,
  delRegion,
  getPost,
  updatePost,
  listPeople,
  listRecords
} from "@/api/access/list";
import { listRegion } from "@/api/system/region";
export default {
  name: "Access",
  dicts: ["sys_people_status"],
  data() {
    return {
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      // 遮罩层
      loading: true,
      value2:'',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // renyuan总条数
      totalPeople: 0,
      totalRecords:0,
      // 岗位表格数据
      postList: [],
      // 人员表格数据
      peopleList: [],
      recordsList:[],
      // 岗位选项
      postOptions: [],
      // 弹出层标题
      title: "",
      time:'',
      time2:'',
      // 是否显示弹出层
      open: false,
      recordsQuery:{
        ip: '',
        loginname: '',
        password:'',
        pageNum: 1,
        pageSize: 10,
        startTime:'',
        endTime:'',
        cardNumber:'',
        employeeNo:'',
        name:''
      },
      recordsQuery2:{
        ip: '',
        loginname: '',
        password:'',
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accessName: undefined,
      },
      table: false,
      table2: false,
      // 表单参数
      form: {
        accessId: "",
      },
      // 表单校验
      rules: {
        accessName: [{ required: true, message: "不能为空", trigger: "blur" }],
        accessIp: [{ required: true, message: "不能为空", trigger: "blur" }],
        loginname: [{ required: true, message: "不能为空", trigger: "blur" }],
        password: [{ required: true, message: "不能为空", trigger: "blur" }],
        region: [{ required: true, message: "不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    this.getRegionList();
    this.setTimes();
  },
  methods: {
    setTimes() {
   var _this = this;
   let yy = new Date().getFullYear();
   let mm = new Date().getMonth() + 1;
   let dd = new Date().getDate();
   let hh = new Date().getHours();
   let mf = new Date().getMinutes() < 10 ? "0" + new Date().getMinutes() : new Date().getMinutes();
   let ss = new Date().getSeconds() < 10 ? "0" + new Date().getSeconds() : new Date().getSeconds();
   _this.form.startTime = yy + "-" + '0'+mm + "-" + dd + " " + hh + ":" + mf + ":" + ss;
   _this.form.startTime2 = yy + "-" + '0'+mm + "-" + dd + " "+'00:00:00'
   var that = this
   console.log(that.time,that.time2);
   that.time = _this.form.startTime
   that.time2 = _this.form.startTime2
   },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listGas(this.queryParams).then((response) => {
        console.log("response,responseresponse", response);
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getRegionList() {
      listRegion(this.queryParams).then((response) => {
        console.log("response,responseresponse", response);
        this.postOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
        /** 导出按钮操作 */
        handleExport() {
      this.download('system/user/exportWorks', {
      }, `user_${new Date().getTime()}.xlsx`)
    },
    // 表单重置
    reset() {
      this.form = {
        accessIp: undefined,
        accessName: undefined,
        loginname: undefined,
        password: undefined,
        region: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleQuery2() {
      this.recordsQuery.startTime = this.value2[0] || this.time2
      this.recordsQuery.endTime = this.value2[1] || this.time
      console.log(this.value2);
      this.recordsQuery.pageNum = 1;
      this.getList2();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.accessId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加门禁";
      this.form.accessId = "";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row, this.ids);
      this.reset();
      const id2 = {
        accessId: row.accessId,
      };
      getPost(id2).then((response) => {
        console.log("response", response);
        this.form = response.data[0];
        this.open = true;
        this.title = "修改气体";
      });
    },
    /** 查看人员按钮操作 */
    handlePeople(row) {
      this.recordsQuery2.ip = row.accessIp,
        this.recordsQuery2.loginname = row.loginname,
        this.recordsQuery2.password = row.password,
      listPeople(this.recordsQuery2).then((response) => {
        console.log("response", response);
        this.peopleList = response.data.data;
        this.totalPeople = response.data.total;
        this.table = true;
      });
    },
    /** 查看打卡记录按钮操作 */
    handleRecords(row) {
      this.setTimes()
        this.recordsQuery.ip = row.accessIp,
        this.recordsQuery.loginname = row.loginname,
        this.recordsQuery.password = row.password,
        this.recordsQuery.startTime = this.time2,
        this.recordsQuery.endTime = this.time,
        this.recordsQuery.cardNumber = row.cardNumber,
        this.recordsQuery.employeeNo = '',
        this.recordsQuery.name = '',
      listRecords(this.recordsQuery).then((response) => {
        this.setTimes()
        this.recordsList = response.data.data;
        this.totalRecords = response.data.total;
        this.table2 = true;
      });
    },
    getList2() {
      listRecords(this.recordsQuery).then((response) => {
        this.recordsList = response.data.data;
        this.totalRecords = response.data.total;
        this.table2 = true;
      });
    },
    getList3() {
      listPeople(this.recordsQuery2).then((response) => {
        console.log("response", response);
        this.peopleList = response.data.data;
        this.totalPeople = response.data.total;
        this.table = true;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.accessId != undefined) {
            addPost(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, type) {
      const rowID = [];
      rowID.push(row.accessId);
      console.log(this.ids);
      if (type === 1) {
        this.$modal
          .confirm('是否确认删除设备编号为"' + row.accessId + '"的数据项？')
          .then(function () {
            return delRegion(rowID);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      } else {
        let that = this;
        this.$modal
          .confirm("是否确认删除选择的数据项？")
          .then(function () {
            return delRegion(that.ids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
    },
  },
};
</script>
